﻿using System;
using System.Collections.Generic;

using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Firefly.Controls;

namespace Firefly {
    static class Program {

        public static event EventHandler UpdateEvent;
        public static event EventHandler OnApplicationQuit;

        public static FormManager WindowManager { get; private set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
//#if DEBUG
            //AllocConsole();
//#endif
            WindowManager = new FormManager();
            StartUpdateThread(); // For asynchronously updating outputs.
            Application.Run(WindowManager);
            }

        private static void StartUpdateThread() {
            Thread updateThread = new Thread(new ThreadStart(delegate() {
                while (true) {
                    if(UpdateEvent != null)
                        UpdateEvent.Invoke(null, null);
                    Thread.Sleep(40);
                }
            }));
            updateThread.Start();
        }

        public static void QuitProgram() {
            if (OnApplicationQuit != null)
                OnApplicationQuit.Invoke(null, null);
            Environment.Exit(0);
        }

//#if DEBUG
        [DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int AllocConsole();
//#endif
    }
}
